﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SubSonic;
using ExtAspNet;


namespace TCMSystem.admin
{
    public partial class role_user_addnew : PageBase
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        #region Module

        /// <summary>
        /// 当前页面所属的模块
        /// </summary>
        public override ModuleType Module
        {
            get
            {
                return ModuleType.CoreRoleUser;
            }
        }

        #endregion

        #region Page_Load

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private void LoadData()
        {
            // 权限检查
            if (!CheckPowerNew())
            {
                CheckPowerFailWithPage();
                return;
            }

            btnClose.OnClientClick = ActiveWindow.GetConfirmHidePostBackReference();

            int id = GetQueryIntValue("id");
            TblMstRole current = TblMstRole.FetchByID(id);
            if (current == null)
            {
                // 参数错误，首先弹出Alert对话框然后关闭弹出窗口
                Alert.Show("参数错误！", String.Empty, ActiveWindow.GetHideReference());
                return;
            }

            BindGrid();
        }


        private void BindGrid()
        {
            SqlQuery q = new Select().From<TblMstUser>();
            q.Where("1").IsEqualTo("1");

            // 在职务名称中搜索
            string searchText = ttbSearchMessage.Text.Trim();
            if (!String.IsNullOrEmpty(searchText))
            {
                q.And(TblMstUser.NameColumn).ContainsString(searchText);
            }

            // 只列出不在当前角色中的用户
            //q.And(TblMstUser.RoleIdColumn).IsNotEqualTo(GetQueryIntValue("id"));
            q.AndExpression(TblMstUser.RoleIdColumn.ColumnName).IsNotEqualTo(GetQueryIntValue("id")).Or(TblMstUser.RoleIdColumn).IsNull().CloseExpression();

            // 在查询添加之后，排序和分页之前获取总记录数
            // Grid1总共有多少条记录
            Grid1.RecordCount = q.GetRecordCount();

            // 排列
            q.OrderBys.Add(GetSortExpression(Grid1, TblMstUser.Schema));

            // 数据库分页
            q.Paged(Grid1.PageIndex + 1, Grid1.PageSize);
            TblMstUserCollection items = q.ExecuteAsCollection<TblMstUserCollection>();

            Grid1.DataSource = items;
            Grid1.DataBind();
        }

        #endregion

        #region Events

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            int roleId = GetQueryIntValue("id");

            // 从每个选中的行中获取ID（在Grid1中定义的DataKeyNames）
            List<int> ids = GetSelectedDataKeyIDs(Grid1);

            // 执行数据库操作
            TblMstUserCollection users = new Select().From<TblMstUser>()
                .Where(TblMstUser.IdColumn).In(ids)
                .ExecuteAsCollection<TblMstUserCollection>();
            foreach (TblMstUser user in users)
            {
                user.RoleId = roleId;
            }
            users.BatchSave();
           
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }


        protected void ttbSearchMessage_Trigger2Click(object sender, EventArgs e)
        {
            ttbSearchMessage.ShowTrigger1 = true;
            BindGrid();
        }

        protected void ttbSearchMessage_Trigger1Click(object sender, EventArgs e)
        {
            ttbSearchMessage.Text = String.Empty;
            ttbSearchMessage.ShowTrigger1 = false;
            BindGrid();
        }

        protected void Grid1_Sort(object sender, GridSortEventArgs e)
        {
            Grid1.SortColumnIndex = e.ColumnIndex;
            BindGrid();
        }

        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
            BindGrid();
        }

        #endregion

    }
}
